package td3.model;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;

import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.MutableTreeNode;
import javax.swing.tree.TreeNode;

import org.xml.sax.SAXException;

public class ContactTreeModel extends DefaultTreeModel {

	private static final long serialVersionUID = 3980051396946380252L;

	public ContactTreeModel(TreeNode root) {
		super(root);
	}

	public static TreeNode openXMLContacts(File file, ContactTreeModel contacts) throws FileNotFoundException, SAXException, IOException {
		TreeNode node = null;
		ContactFacility contactFacility = new ContactFacility();
		node = contactFacility.parse(file, contacts).root;
		return node;
	}

	public String toXml() throws Exception {
		ContactFacility contactFacility = new ContactFacility();
		return contactFacility.writeToXml(root);
	}

	public void addEmptyContactToRoot() {
		insertNodeInto(new DefaultMutableTreeNode(new Contact()),
				(MutableTreeNode) root.getChildAt(0), 0);
	}

}
